package org.algorithm.prim;

import org.datastructure.firstday.graph.Graph;

import java.util.Arrays;

public class PrimAlgorithm {

    public static void main(String[] args) {
        //测试看看图是否创建ok
        char[] data = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        int verxs = data.length;
        //邻接矩阵的关系使用二维数组表示,10000这个大数，表示两个点不联通
        int[][] weight = new int[][]{
                {10000, 5, 7, 10000, 10000, 10000, 2},
                {5, 10000, 10000, 9, 10000, 10000, 3},
                {7, 10000, 10000, 10000, 8, 10000, 10000},
                {10000, 9, 10000, 10000, 10000, 4, 10000},
                {10000, 10000, 8, 10000, 10000, 5, 4},
                {10000, 10000, 10000, 4, 5, 10000, 6},
                {2, 3, 10000, 10000, 4, 6, 10000},};

        //创建MGraph对象
        MGraph graph = new MGraph(verxs);
        //创建一个MinTree对象
        MinTree minTree = new MinTree();
        minTree.createGraph(graph, verxs, data, weight);
        //输出
        minTree.showGraph(graph);
        minTree.prim(graph,0);
    }
}

class MinTree {

    public void createGraph(MGraph graph, int vertexs, char[] data, int[][] weight) {
        for (int i = 0; i < vertexs; i++) {
            graph.data[i] = data[i];
            for (int j = 0; j < vertexs; j++) {
                graph.weight[i][j] = weight[i][j];
            }
        }
    }

    public void showGraph(MGraph graph) {
        for (int[] ints : graph.weight) {
            for (int anInt : ints) {
                System.out.printf("%-7d", anInt);
            }
            System.out.println();
        }
    }


    public void prim(MGraph graph, int vertexs) {
        // TODO: 2022/4/6 java 默认将未初始化的int数组全部为0；
        int[] isVisited = new int[graph.vertexs];

        isVisited[vertexs] = 1;
        // TODO: 2022/4/6 设置一个最小权重；
        int minWeight = 10000;// TODO: 2022/4/6  默认为最大值，也可以每次循环的时候初始值设最大；
        int h1 = -1, h2 = -1;// TODO: 2022/4/6

        // TODO: 2022/4/6  这个循环的次数是表示顶点数-1是边数
        for (int edges = 0; edges < graph.vertexs - 1; edges++) {
            minWeight = 10000;
            for (int i = 0; i < graph.vertexs; i++) {
                for (int j = 0; j < graph.vertexs; j++) {

                    // TODO: 2022/4/6  实现已经被设为1(所有已经选为与上一个顶点产生最小边的权值顶点)与所有可能的顶点(isVisited=0)
                    //  边的权值比较，且取得最小权值

                    //  isVisited[i]=1,就是已经产生的最小权值边的顶点
                    //  isVisitied[j]=0,所有未确定下来的顶点
                    if (isVisited[i] == 1 && isVisited[j] == 0 && graph.weight[i][j] < minWeight) {
                        minWeight = graph.weight[i][j];
                        h1=i;
                        h2 = j;
                    }
                }
            }
            //找到一条边是最小
            System.out.println("边<" + graph.data[h1] + "," + graph.data[h2] + "> 权值:" + minWeight);
            //将当前这个结点标记为已经访问
            isVisited[h2] = 1;
        }
    }
}

class MGraph {
    int vertexs;
    char[] data;
    int[][] weight;

    public MGraph(int vertexs) {
        this.vertexs = vertexs;
        data = new char[vertexs];
        weight = new int[vertexs][vertexs];
    }
}

